import Dialog from '@vant/weapp/dialog/dialog';
var util = require('../../utils/util.js')

Page({
  data: {
    active: 0,
    active2: 0,
    listData:[
      {"code":"早","dc":"4","wc":"4","jy":"50"},
      {"code":"中","dc":"15","wc":"8","jy":"120"},
      {"code":"晚","dc":"4","wc":"4","jy":"4"},
      {"code":"总结","dc":"23","wc":"4","jy":"260"},
    ],
    title: '', // 窗口名称
    value: '',
    orderinfo:[], // 订单信息
    mealinfo: [], // 饭菜信息
    allOrdernum:0, //所有订单数
    ordernum:0, //未取餐订单数
    imgurl:[
      "https://img-blog.csdnimg.cn/2021050921352216.png",
      "https://img-blog.csdnimg.cn/2021050921354479.png",
      "https://img-blog.csdnimg.cn/2021050921363561.png",
      "https://img-blog.csdnimg.cn/20210509213647572.png"
    ],
    imgurl1: "https://img-blog.csdnimg.cn/2021050921352216.png",
    imgurl2: "https://img-blog.csdnimg.cn/2021050921354479.png"
  },

  //  时间升序
  clickup() {
    var that = this
    let canteen_id2 = wx.getStorageSync('canteen').canteen_id;
    let window_id2 = wx.getStorageSync('window_id');
    that.setData({
      imgurl1: that.data.imgurl[2],
      imgurl2: that.data.imgurl[1],
    })
    
    wx.cloud.callFunction({
      name: 'lu',
      data: {
        action: 'orderinfobyasc',
        canteen_id: canteen_id2,
        window_id: window_id2
      },
      success(res) {
        that.setData({
          orderinfo: res.result.data,
        })
        console.log("订单云函数获取数据成功",res)
      },
      fail(res) {
        console.log("函数获取数据失败",res)
      }
    })
  },

  // 时间降序
  clickdown() {
    var that = this
    let canteen_id2 = wx.getStorageSync('canteen').canteen_id;
    let window_id2 = wx.getStorageSync('window_id');
    that.setData({
      imgurl1: that.data.imgurl[0],
      imgurl2: that.data.imgurl[3],
    })
    wx.cloud.callFunction({
      name: 'lu',
      data: {
        action: 'orderinfobydesc',
        canteen_id: canteen_id2,
        window_id: window_id2
      },
      success(res) {
        that.setData({
          orderinfo: res.result.data,
        })
        console.log("订单云函数获取数据成功",res)
      },
      fail(res) {
        console.log("函数获取数据失败",res)
      }
    })
  },
  
  onChange(event) {
    this.setData({ active: event.detail });
  },

  // 下面的tab栏
  onChange2(event) {
    console.log(event.detail)
    util.time()
    this.setData({ active2: event.detail });
  },

  // 搜索框变化
  onChange3(event) {
    this.setData({
      value: event.detail,
    });
  },

  onSearch() {
    var that = this
    // console.log(that.data.value.length)
    let canteen_id2 = wx.getStorageSync('canteen').canteen_id;
    let window_id2 = wx.getStorageSync('window_id');
    if(that.data.value.length == 0) {
      wx.cloud.callFunction({
        name: 'lu',
        data: {
          action: 'orderinfo',
          canteen_id: canteen_id2,
          window_id: window_id2
        },
        success(res) {
          that.setData({
            orderinfo: res.result.data,
            allOrdernum: res.result.data.length
          })
          console.log("订单云函数获取数据成功",res)
        },
        fail(res) {
          console.log("函数获取数据失败",res)
        }
      })
    }else{
      wx.cloud.callFunction({
        name: 'lu',
        data: {
          action: 'orderbycondition',
          value: that.data.value,
          canteen_id: canteen_id2,
          window_id: window_id2
        },
        success(res) {
          that.setData({
            orderinfo: res.result.data,
          })
          console.log("订单云函数获取数据成功",res)
        },
        fail(res) {
          console.log("函数获取数据失败",res)
        }
      })
    }
  },
  onClick() {
    console.log(this.data.value)
    this.onSearch()
  },

  // 点击已取餐
  clickstatus:function(event) {
    var that = this
    let id = event.currentTarget.dataset.id
    let canteen_id2 = wx.getStorageSync('canteen').canteen_id;
    let window_id2 = wx.getStorageSync('window_id');
    // console.log(id)
    Dialog.confirm({
      title: '是否确认已取餐',
      message: '确认后不可更改',
    })
    .then(() => {
      wx.cloud.callFunction({
        name: 'lu',
        data: {
          action: 'orderupdate',
          id: id,
          order_status: "1"
        },
        success(res) {
          console.log("订单云函数修改数据成功",res)
          wx.cloud.callFunction({
            name: 'lu',
            data: {
              action: 'orderinfo',
              canteen_id: canteen_id2,
              window_id: window_id2
            },
            success(res) {
              that.setData({
                orderinfo: res.result.data,
              })
              console.log("订单云函数获取数据成功",res)
            },
            fail(res) {
              console.log("函数获取数据失败",res)
            }
          })
        },
        fail(res) {
          console.log("函数获取数据失败",res)
        }
      })
    })
    .catch(() => {
      // on cancel
    });
    

  },

  newmeal:function() {
    wx.navigateTo({
      url: './addmeal/addmeal',
    })
  },
  editmeal:function(event) {
    console.log(event.currentTarget.dataset.id)
    wx.navigateTo({
      url: './editmeal/editmeal?id=' + event.currentTarget.dataset.id,
    })
  },

  goOrder:function() {
    wx.cloud.callFunction({
      name: 'lu',
      data: {
        action: 'order',
        canteen_id: canteen_id2,
        window_id: window_id2
      },
      success(res) {
        let meal = res.result.data
        that.setData({
          ordernum: meal.length,
        })
        console.log("云函数获取待取餐数据成功",meal)
      },
      fail(res) {
        console.log("函数获取待取餐数据失败",res)
      }
    })
    wx.navigateTo({
      url: './reserve/reserve?status=0',
    })
  },
  goAllOrder:function() {
    wx.navigateTo({
      url: './reserve/reserve?status=1',
    })
  },
  onLoad: function(options) {
    let canteen_id2 = wx.getStorageSync('canteen').canteen_id;
    let window_id2 = wx.getStorageSync('window_id');
    console.log(canteen_id2 + '-' + window_id2)
    //获取当前时间
    var TIME = util.formatTime(new Date());
    this.setData({
      time: TIME,
    });
    var that = this
    if(!wx.getStorageSync('login')) {
      wx.redirectTo({
        url: '/pages/manager/login/login',
      })
    } else {
      // -----------------------------------订单页面信息
      // 窗口信息
      that.setData({
        title: wx.getStorageSync('canteen').canteen_name + "  " + wx.getStorageSync("window_name"),
        active2: options.status
      })
      // 所有订单
      wx.cloud.callFunction({
        name: 'lu',
        data: {
          action: 'orderinfo',
          canteen_id: canteen_id2,
          window_id: window_id2
        },
        success(res) {
          that.setData({
            orderinfo: res.result.data,
            allOrdernum: res.result.data.length
          })
          console.log("订单云函数获取数据成功",res)
        },
        fail(res) {
          console.log("函数获取数据失败",res)
        }
      })
      // 未取餐订单
      wx.cloud.callFunction({
        name: 'lu',
        data: {
          action: 'order',
          canteen_id: canteen_id2,
          window_id: window_id2
        },
        success(res) {
          let meal = res.result.data
          that.setData({
            ordernum: meal.length,
          })
          console.log("云函数获取待取餐数据成功",meal)
        },
        fail(res) {
          console.log("函数获取待取餐数据失败",res)
        }
      })
      // -----------------------------------饭菜页面信息
      // 所有饭菜
      wx.cloud.callFunction({
        name: 'lu',
        data: {
          action: 'mealinfo',
          canteen_id: canteen_id2,
          window_id: window_id2
        },
        success(res) {
          that.setData({
            mealinfo: res.result.data
          })
          console.log("饭菜云函数获取数据成功",res)
        },
        fail(res) {
          console.log("函数获取数据失败",res)
        }
      })
    }
  },
   /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    util.time()
  },
})
